export 'ui/widget/runtime_settings.dart';

import 'dart:async';

import 'package:flutter/services.dart';
import 'package:get/get.dart';

import 'util/util.dart';

class DisableBatteryOptimization {
  DisableBatteryOptimization._();

  static bool _hasInit = false;
  static void init(){
    if(!_hasInit){
      _hasInit = true;
      if(Get.translations.isEmpty){
        Get.addTranslations(keys);
      }else{
        Map<String, Map<String, String>> translations =
        Get.translations.map((k, v){
          if(keys.containsKey(k)){
            return MapEntry(k, Map<String, String>.of(v)..addAll(keys[k]!));
          }else{
            return MapEntry(k, v);
          }
        });
        Get.clearTranslations();
        Get.addTranslations(translations);
      }

    }

  }

  static const MethodChannel _channel =
      const MethodChannel('in.jvapps.disable_battery_optimization');

  static Future<bool?> showEnableAutoStartSettings(
      String dialogTitle, String dialogBody) async {
    return await _channel.invokeMethod<bool>(
        'showEnableAutoStart', <dynamic>[dialogTitle, dialogBody]);
  }

  static Future<bool?> showDisableManufacturerBatteryOptimizationSettings(
      String dialogTitle, String dialogBody) async {
    return await _channel.invokeMethod<bool>('showDisableManBatteryOptimization',
        <dynamic>[dialogTitle, dialogBody]);
  }

  static Future<bool?> showDisableBatteryOptimizationSettings() async {
    return await _channel.invokeMethod<bool>('showDisableBatteryOptimization');
  }

  static Future<bool?> showDisableAllOptimizationsSettings(
      String autoStartTitle,
      String autoStartBody,
      String manBatteryTitle,
      String manBatteryBody) async {
    return await _channel.invokeMethod<bool>('disableAllOptimizations', <dynamic>[
      autoStartTitle,
      autoStartBody,
      manBatteryTitle,
      manBatteryBody
    ]);
  }

  static Future<bool?> get isAutoStartEnabled async {
    return await _channel.invokeMethod<bool>("isAutoStartEnabled");
  }

  static Future<bool?> get isBatteryOptimizationDisabled async {
    return await _channel.invokeMethod<bool>("isBatteryOptimizationDisabled");
  }

  static Future<bool?> get isManufacturerBatteryOptimizationDisabled async {
    return await _channel.invokeMethod<bool>("isManBatteryOptimizationDisabled");
  }

  static Future<bool?> get isAllBatteryOptimizationDisabled async {
    return await _channel.invokeMethod<bool>("isAllOptimizationsDisabled");
  }

  /// to BatteryOptimize setting
  static Future<bool?> toBatteryOptimize() async {
    return await _channel.invokeMethod<bool>("toBatteryOptimize");
  }

  /// to AutoStart setting
  static Future<bool?> toAutoStart() async {
    return await _channel.invokeMethod<bool>("toAutoStart");
  }
}
